package net.xxcxcxcx.xc_enhancedmob.skills.basics.individual.rare;

import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.mob.MobEntity;
import net.minecraft.nbt.NbtCompound;
import net.xxcxcxcx.xc_enhancedmob.config.skill.SkillConfig;
import net.xxcxcxcx.xc_enhancedmob.skills.BaseSkill;
import net.xxcxcxcx.xc_enhancedmob.skills.SkillRarity;

public class DashSkill extends BaseSkill {

    public DashSkill(int level) {
        super(level);
    }

    public DashSkill(NbtCompound nbtCompound) {
        super(nbtCompound);
    }

    public DashSkill() {
    }

    @Override
    public SkillConfig getDefaultConfig() {
        SkillConfig skillConfig = super.getDefaultConfig();
        skillConfig.setMaxlevel(10);
        skillConfig.setCooldown(200);
        skillConfig.setMessage(getConfigMessage(skillConfig));
        return skillConfig;
    }

    @Override
    public String getConfigMessage(SkillConfig skillConfig) {
        if (skillConfig == null) return super.getConfigMessage(null);
        return String.format("冲锋技能，每隔（ %d -技能等级*5）tick直奔目标发起一次冲锋。", skillConfig.getCooldown());
    }

    public SkillRarity getSkillRarity() {
        return SkillRarity.Rare;
    }

    @Override
    public boolean canUseAbility(MobEntity mobEntity) {
        return (mobEntity.getTarget() != null && getSkilltime() < mobEntity.world.getTime());
    }

    @Override
    public boolean onAbilityUse(MobEntity mobEntity) {
        if (canUseAbility(mobEntity)) {
            setSkilltime(mobEntity.world.getTime() + getCooldown() - getSkillLevel() * 5L);
            LivingEntity target = mobEntity.getTarget();
            double X = target.getX() - (mobEntity.getX());
            double Y = target.getY() - (mobEntity.getY()) + 1;
            double Z = target.getZ() - (mobEntity.getZ());
            Double T = Math.sqrt(X * X + Y * Y + Z * Z);//用于单位化向量
            mobEntity.setVelocity(X / T / 2 * getSkillLevel(), Y / T / 2 * getSkillLevel(), Z / T / 2 * getSkillLevel());
        }
        return true;
    }
}
